/**
 * Copyright (C), 2015-2018
 * FileName: FilterConfiguration
 * Author:   Dayu Hou
 * Date:     2018/1/10 11:38
 * Description:
 * History:
 * <author>          <time>         <desc>
 */
package com.apache.passport.tomcat;

import com.apache.passport.common.SsoServerFilter;
import com.apache.plateform.interceptor.TokenInterceptor;
import com.apache.rpc.StartSocketPortListener;
import com.apache.tools.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Dayu Hou
 * @since 1.0.0
 */
@Configuration
public class FilterConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    private Environment env;

    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截

        registry.addInterceptor(new TokenInterceptor()).addPathPatterns("/**");
        // registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**");

        super.addInterceptors(registry);
    }

    /**
     * 加载listener
     */
    @Bean
    public StartSocketPortListener startSocketPortListener() {
        String socketPort = StrUtil.doNull(env.getProperty("rpc.service.port"), "20808");
        StartSocketPortListener listener =  new StartSocketPortListener(socketPort);
        listener.registry();
        return listener;
    }

    @Bean
    public FilterRegistrationBean encodingFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new CharacterEncodingFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("encoding", "UTF-8");
        registration.addInitParameter("forceEncoding", "true");
        registration.setName("encodingFilter");
        registration.setOrder(1);
        return registration;
    }

    //    @Bean
    //    public FilterRegistrationBean defaultBaseSecurityFilter() {
    //        FilterRegistrationBean registration = new FilterRegistrationBean();
    //        registration.setFilter(new CharacterEncodingFilter());
    //        registration.addUrlPatterns("*.action", "*.jsp");
    //        registration.addInitParameter("securityFilterList",
    //                "com.apache.security.filter.SqlCookieFilter,com.apache.security.filter.HttpRefererFilter");
    //        registration.addInitParameter("errorPage", "/common/403.jsp");
    //        registration.setName("defaultBaseSecurityFilter");
    //        registration.setOrder(4);
    //        return registration;
    //    }

    @Bean
    public FilterRegistrationBean ssoServerFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new SsoServerFilter());
        registration.addUrlPatterns("/*");
        registration.setName("ssoServerFilter");
        registration.setOrder(2);
        return registration;
    }
}